package com.junjiao.util.java;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import freemarker.ext.beans.BeansWrapper;
import freemarker.template.TemplateHashModel;
import freemarker.template.TemplateModelException;

public class BeansWrapperTag extends TagSupport {

	private static final long serialVersionUID = 5038932901453510598L;
	private Log log = LogFactory.getLog(BeansWrapper.class);
	private String classname;
	private String name;

	public String getClassname() {
		return classname;
	}

	public void setClassname(String className) {
		this.classname = className;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public int doStartTag() throws JspException {
		try {
			BeansWrapper wrapper = BeansWrapper.getDefaultInstance();
			TemplateHashModel staticModels = wrapper.getStaticModels();
			TemplateHashModel cache = (TemplateHashModel) staticModels
					.get(classname);
			pageContext.getRequest().setAttribute(name, cache);
		} catch (TemplateModelException e) {
			log.error(e.getMessage());
		}
		return super.doStartTag();
	}

}
